﻿using System;
using System.Net;
using System.IO;
using System.Text;

namespace Common.Helpers
{
    class HttpHelper
    {
        public static string Post(string url, string postData)
        {
            //url = "http://example.com/login"; // 登录接口URL
            //postData = "{\"username\":\"your_username\",\"password\":\"your_password\"}"; // 请求体内容，JSON格式

            string userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.50"; // 设置User-Agent

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.UserAgent = userAgent;
            request.ContentType = "application/json";
            request.ContentLength = postData.Length;
            request.Headers.Add("", "");

            using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(postData);
            }

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string result = string.Empty;

            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                result = reader.ReadToEnd();
            }

            //Console.WriteLine(result); // 打印登录结果

            response.Close();
            return result;
        }
    }
}
